package com.example.flashsales.controller;

import com.example.flashsales.entity.Result;
import com.example.flashsales.entity.dto.OrderCarDetailsDto;
import com.example.flashsales.entity.vo.OrderCarDetailsVo;
import com.example.flashsales.service.impl.OrderCarServiceImpl;
import com.example.flashsales.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/user/orderCar")
public class OrderCarController {

    @Autowired
    OrderCarServiceImpl orderCarService;

    @Autowired
    UserServiceImpl userService;


    /**
     * 用户添加商品进入购物车
     *
     * @param httpServletRequest
     * @param orderCarDetailsDto
     * @return
     */
    @PostMapping("/addGoodsInOrderCar")
    @ResponseBody
    public Result addGoodsInOrderCar(HttpServletRequest httpServletRequest, @RequestBody OrderCarDetailsDto orderCarDetailsDto) {
        if (orderCarDetailsDto == null){
            return Result.error("数据不能为空");

        }
        long id = Integer.parseInt(httpServletRequest.getAttribute("id") + "");
        final String s = userService.addGoodsInOrderCar(id, orderCarDetailsDto);
        if (s.equals("添加成功")) {
            return Result.ok(s);
        }
        return Result.error(s);
    }

    /**
     * 用户修改购物车里面商品的数量
     *
     * @param httpServletRequest
     * @param orderCarDetailsDto
     * @return
     */
    @PostMapping("/changeOrderCarGoodsCount")
    @ResponseBody
    public Result changeOrderCarGoodsCount(HttpServletRequest httpServletRequest, @RequestBody OrderCarDetailsDto orderCarDetailsDto) {
        if (orderCarDetailsDto == null){
            return Result.error("数据不能为空");
        }
        long id = Integer.parseInt(httpServletRequest.getAttribute("id") + "");
        final String s = userService.changeOrderCarGoodsCount(id, orderCarDetailsDto);
        if (s.equals("修改成功")) {
            return Result.ok(s);
        }
        return Result.error(s);

    }


    /**
     * 用户清空自己的购物车
     *
     * @param httpServletRequest
     * @return
     */
    @ResponseBody
    @GetMapping("/cleanOrderCar")
    public Result cleanOrderCar(HttpServletRequest httpServletRequest) {
        long id = Integer.parseInt(httpServletRequest.getAttribute("id") + "");
        final String s = userService.cleanOrderCar(id);
        if (s.equals("清空成功")) {
            return Result.ok(s);
        }
        return Result.error(s);

    }

    /**
     * 用户查看自己的购物车
     *
     * @param httpServletRequest
     * @return
     */
    @ResponseBody
    @GetMapping("/viewUserOrderCarMessage")
    public Result viewUserOrderCarMessage(HttpServletRequest httpServletRequest) {
        long id = Integer.parseInt(httpServletRequest.getAttribute("id") + "");
        final List<OrderCarDetailsVo> list = userService.viewUserOrderCarMessage(id);
        return Result.ok("查看成功").setData(list);

    }


    /**
     * 用户删除购物车里面的某条数据
     *
     * @param httpServletRequest
     * @param goodsId
     * @return
     */
    @ResponseBody
    @GetMapping("/deleteGoodsInOrderCar")
    public Result deleteGoodsInOrderCar(HttpServletRequest httpServletRequest, Long goodsId) {
        if (goodsId == null){
            return Result.error("数据不能为空");

        }
        long id = Integer.parseInt(httpServletRequest.getAttribute("id") + "");
        OrderCarDetailsDto orderCarDetailsDto = new OrderCarDetailsDto();
        orderCarDetailsDto.setGoodsId(goodsId);
        final String s = userService.deleteGoodsInOrderCar(id, orderCarDetailsDto);
        if (s.equals("删除成功")) {
            return Result.ok(s);
        }
        return Result.error(s);

    }
}
